<template>
  <div class="login-container">
    <div class="login-box">
      <h1 class="title">智能财务共享中心</h1>
      <el-form
          ref="loginFormRef"
          :model="loginForm"
          :rules="rules"
          class="login-form"
      >
        <el-form-item prop="username">
          <el-input
              v-model="loginForm.username"
              placeholder="用户名"
              prefix-icon="User"
              size="large"
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
              v-model="loginForm.password"
              placeholder="密码"
              prefix-icon="Lock"
              show-password
              size="large"
              type="password"
              @keyup.enter="handleLogin"
          />
        </el-form-item>

        <div class="login-options">
          <el-checkbox v-model="loginForm.remember">记住我</el-checkbox>
          <el-button link type="primary" @click="forgotPassword">忘记密码？</el-button>
        </div>

        <div class="form-actions">
          <el-button
              :loading="loading"
              class="submit-button"
              size="large"
              type="primary"
              @click="handleLogin"
          >
            登录
          </el-button>
          <div class="register-link">
            还没有账号？
            <router-link to="/register">立即注册</router-link>
          </div>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts" setup>
import {onMounted, reactive, ref} from 'vue';
import {ElMessage} from 'element-plus';
import {useRouter} from 'vue-router';
import {login} from '@/api/user.ts';
import {getRememberUsername, setRememberUsername, setToken} from '@/utils/storage.ts';

const router = useRouter();
// const route = useRoute();
const loginFormRef = ref();

// 登录表单数据
const loginForm = reactive({
  username: '',
  password: '',
  remember: true
});

// 加载状态
const loading = ref(false);

// 登录处理
const handleLogin = async () => {
  // 每次登录前清除表单验证状态
  if (!loginFormRef.value) return;

  // 表单验证
  try {
    await loginFormRef.value.validate();
    loading.value = true;

    try {
      // 调用真实的登录API
      const res = await login({
        username: loginForm.username,
        password: loginForm.password
      });

      if (res.code === 200 && res.data) {
        // 保存token和用户名
        setToken(res.data.token);
        if (loginForm.remember) {
          setRememberUsername(loginForm.username);
        }

        // 登录成功提示
        ElMessage.success('登录成功');

        // 跳转到系统首页
        router.push('/dashboard/order-list');
      } else {
        ElMessage.error(res.message || '登录失败');
        console.error('登录响应错误:', res);
      }
    } catch (error: any) {
      console.error('登陆错误', error);
      ElMessage.error(error.message || '登录失败，请重试');
    } finally {
      loading.value = false;
    }
  } catch (error) {
    console.error('表单验证失败:', error);
  }
};

// 初始化页面 - 简化逻辑
onMounted(() => {
  // 获取记住的用户名
  const savedUsername = getRememberUsername();
  if (savedUsername) {
    loginForm.username = savedUsername;
    loginForm.remember = true;
  }

  // 设置登录页面标题
  document.title = '登录 - 智能财务共享中心';
});

// 表单验证规则
const rules = {
  username: [
    {required: true, message: '请输入用户名', trigger: 'blur'}
  ],
  password: [
    {required: true, message: '请输入密码', trigger: 'blur'}
  ]
};

// 忘记密码处理
const forgotPassword = () => {
  ElMessage.info('请联系系统管理员重置密码');
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f0f2f5;
  background-image: url('');
}

.login-box {
  width: 400px;
  padding: 40px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}

.title {
  text-align: center;
  color: #409eff;
  margin-bottom: 30px;
  font-size: 28px;
}

.login-form {
  width: 100%;
}

.login-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.form-actions {
  margin-top: 20px;
}

.submit-button {
  width: 100%;
  margin-bottom: 16px;
}

.register-link {
  text-align: center;
  margin-top: 16px;
}

@media screen and (max-width: 576px) {
  .login-box {
    width: 90%;
    padding: 20px;
  }
}
</style> 